import * as _ from "lodash"
import { PageList } from "../data"
import httpClient from "../httpclient"

export enum BannerPage {
  HomePage = "home_page",
  CategoryPage = "category_page",
  BrandPage = "brand_page",
  SearchPage = "search_page"
}

export enum BannerDateType {
  Always = "always",
  Custom = "custom"
}

export enum BannerLocation {
  Top = "top",
  Bottom = "bottom"
}

export enum BannerVisibility {
  Visible = "visible", Hidden = "hidden"
}

export class Banner {
  public id: string
  public name: string
  public content: string
  public page: BannerPage
  public dateType: BannerDateType
  public dateFrom: string
  public dateTo: string
  public visibility: BannerVisibility
}


export class BannerQuery {
  private params = {}

  public page(page: number) {
    this.params["page"] = page
    return this
  }

  public limit(limit: number) {
    this.params["limit"] = limit
    return this
  }

  public pages(pages: BannerPage[]): BannerQuery {
    this.params["pages"] = pages
    return this
  }

  public dateTypes(dateTypes: BannerDateType[]): BannerQuery {
    this.params["date_types"] = dateTypes
    return this
  }

  public locations(locations: BannerLocation[]): BannerQuery {
    this.params["locations"] = locations
    return this
  }

  public visibilities(visibilities: BannerVisibility[]): BannerQuery {
    this.params["visibilities"] = visibilities
    return this
  }

  public list(): Promise<PageList<Banner>> {
    return httpClient
      .get(`/v1/banners`, { params: this.params })
      .then(({ data }) => {
        return _.assign(PageList.empty(), data, {
          elements: _.map(data.elements, banner => _.assign(new Banner(), banner)),
        })
      })
  }
}

export class BannerServiceV1 {

  public static getBanners(): BannerQuery {
    return new BannerQuery()
  }
}


